﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Entity;
using System.Transactions;
using DL;

namespace BL.Service
{
    public class tbl_ExportService : ServiceBase
    {
        private List<NumberDTO> UpdateDataOverLimit(List<NumberDTO> numlist, string UpdateType, decimal limit)
        {
            List<NumberDTO> UpdateNUmber = new List<NumberDTO>();
            switch (UpdateType)
            {
                case Enum.UpdateType.Over:
                    UpdateNUmber = (from item in numlist
                                    where item.Over_Price > limit
                                    select item).ToList();
                    foreach (NumberDTO item in UpdateNUmber)
                    {
                        item.Cut_Out = item.Over_Price - limit;
                        item.Over_Price = limit;
                    }
                    break;
                case Enum.UpdateType.Under:
                    UpdateNUmber = (from item in numlist
                                    where item.Under_Price > limit
                                    select item).ToList();
                    foreach (NumberDTO item in UpdateNUmber)
                    {
                        item.Cut_Out = item.Under_Price - limit;
                        item.Under_Price = limit;
                    }
                    break;
                case Enum.UpdateType.Tod:
                    UpdateNUmber = (from item in numlist
                                    where item.Tod_Price > limit
                                    select item).ToList();
                    foreach (NumberDTO item in UpdateNUmber)
                    {
                        item.Cut_Out = item.Tod_Price - limit;
                        item.Tod_Price = limit;
                    }
                    break;
            }
            return UpdateNUmber.Where(e => e.Over_Price > 0 || e.Under_Price > 0 || e.Tod_Price > 0).ToList();
        }
        private void ExportNumberToDb(List<NumberDTO> insertNum, string userName, string numberType, string UpdateType)
        {
            tbl_NumberServer Numberservice = new tbl_NumberServer();

            DateTime nowDate = this.GetDateGobal();
            foreach (NumberDTO AddItem in insertNum)
            {

                #region AddTo Export
                tbl_Export InsertItem = new tbl_Export()
                {
                    CreateBy = userName,
                    CreateDate = nowDate,
                    ExportDate = nowDate,
                    Number = AddItem.Number,
                    //NumType = AddItem.NumType,
                    UpdateBy = userName,
                    UpdateDate = nowDate
                };
              
                switch (UpdateType)
                {
                    case Enum.UpdateType.Over:
                        InsertItem.Over_Price = AddItem.Cut_Out;
                        break;
                    case Enum.UpdateType.Under:
                        InsertItem.Under_Price = AddItem.Cut_Out;
                        break;
                    case Enum.UpdateType.Tod:
                        InsertItem.Tod_Price = AddItem.Cut_Out;
                        break;
                }
                this.Context.AddTotbl_Export(InsertItem);

                #endregion

                #region Add To Number

                tbl_number numberAddDB = new tbl_number()
                {

                    CreateBy = userName,
                    CreateDate = nowDate,
                    Number = AddItem.Number,
                    NumType = AddItem.NumType,
                    Over_Price = AddItem.Over_Price,
                    Tod_Price = AddItem.Tod_Price,
                    Under_Price = AddItem.Under_Price,
                    UpdateBy = userName,
                    UpdateDate = nowDate
                };

                this.Context.AddTotbl_number(numberAddDB);

                #endregion
            }
            this.Context.SaveChanges();
        }
        public void ExportNumber(decimal limit, string numberType, string UserName)
        {

            TransactionOptions tranOpt = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted };
            using (var trans = new TransactionScope(TransactionScopeOption.Required, tranOpt))
            {
                string where = string.Empty;
                string updateType = string.Empty;
                List<NumberDTO> dbNUmber = new List<NumberDTO>();
                List<NumberDTO> UpdateNUmber = new List<NumberDTO>();

                tbl_NumberServer Numberservice = new tbl_NumberServer();
                switch (numberType)
                {
                    case Enum.NumberType.Over3:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=3 AND NOT(Number LIKE '%-%') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Over, limit);
                        updateType = Enum.UpdateType.Over;
                        break;
                    case Enum.NumberType.Tod3:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=3 AND NOT(Number LIKE '%-%') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Tod, limit);
                        updateType = Enum.UpdateType.Tod;
                        break;
                    case Enum.NumberType.Under3:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=3 AND NOT(Number LIKE '%-%') AND NumType='NU' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Under, limit);
                        updateType = Enum.UpdateType.Under;
                        break;
                    case Enum.NumberType.Over2:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=2 AND NOT(Number LIKE '%-%') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Over, limit);
                        updateType = Enum.UpdateType.Over;
                        break;
                    case Enum.NumberType.Under2:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=2 AND NOT(Number LIKE '%-%') AND NumType='NU' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Under, limit);
                        updateType = Enum.UpdateType.Under;
                        break;
                    case Enum.NumberType.Tod2:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=2 AND NOT(Number LIKE '%-%') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Tod, limit);
                        updateType = Enum.UpdateType.Tod;
                        break;
                    case Enum.NumberType.TwinFront:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=3 AND Number LIKE '%-' AND NOT(Number LIKE '-%')AND NOT(Number LIKE '%--') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Over, limit);
                        updateType = Enum.UpdateType.Over;
                        break;
                    case Enum.NumberType.TwinStretch:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)=3 AND Number LIKE '%-%' AND NOT(Number LIKE '-%') AND NOT(Number LIKE '%-') AND NumType='NO' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Over, limit);
                        updateType = Enum.UpdateType.Over;
                        break;

                    case Enum.NumberType.Under1:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = " WHERE LEN(Number)<=2 AND (Number LIKE '%-' OR  Number LIKE '-%' OR LEN(Number)=1)  AND NumType='NU' ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Under, limit);
                        updateType = Enum.UpdateType.Under;
                        break;
                    case Enum.NumberType.Over1:
                        dbNUmber = Numberservice.get_numberShow(numberType);
                        where = "  WHERE (LEN(Number)=1  OR((Number LIKE '%--' OR  Number LIKE '--%' OR  Number LIKE '%-%' )AND LEN(Number)=3 )) AND NumType='NO'  ";
                        UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Over, limit);
                        updateType = Enum.UpdateType.Over;
                        break;
                   
                }

                if (numberType == Enum.NumberType.FiveNumber || numberType == Enum.NumberType.FourNumber) {
                    dbNUmber = Numberservice.get_numberShow(Enum.NumberType.FiveNumber);
                    dbNUmber.AddRange(Numberservice.get_numberShow(Enum.NumberType.FourNumber));
                    where = " WHERE LEN(Number)>=4  AND NumType='NO' ";
                    UpdateNUmber = UpdateDataOverLimit(dbNUmber, Enum.UpdateType.Tod, limit);
                    updateType = Enum.UpdateType.Tod;
                }

                string incondition=string.Empty;

                StringBuilder sqlDel = new StringBuilder();
                sqlDel.AppendLine(" DELETE FROM tbl_number ");
                sqlDel.AppendLine(where);
                sqlDel.AppendLine("AND Number IN(");
                foreach (NumberDTO numDel in UpdateNUmber)
                {
                    incondition += string.Format("'{0}',", numDel.Number);
                }
                if (incondition.Length > 1)
                {
                    incondition = incondition.Substring(0, incondition.Length - 1);
                    sqlDel.Append(incondition);
                    sqlDel.AppendLine(")");
                    this.Context.ExecuteStoreCommand(sqlDel.ToString());
                    this.ExportNumberToDb(UpdateNUmber, UserName, numberType, updateType);
                }
                trans.Complete();

            }
        }
        public List<TimeShow> GetCutTime() {
            List<TimeShow> result = new List<TimeShow>();
            StringBuilder sql = new StringBuilder();
            sql.AppendLine(" SELECT ExportDate FROM tbl_Export GROUP BY ExportDate ");


            result = this.Context.ExecuteStoreQuery<TimeShow>(sql.ToString()).ToList();
             return result;
        }
    }
}
